//
//  CommonUpLoadImageView.m
//  DouLinBusiness
//
//  Created by ypj on 2021/4/9.
//

#import "CommonUpLoadImageView.h"
@interface CommonUpLoadImageView ()
@property(nonatomic,assign)CGFloat itemW;
@property(nonatomic,assign)CGFloat itemH;
@end
@implementation  CommonUpLoadImageView
-(instancetype)initWithFrame:(CGRect)frame withSectionTitle:(NSString *)titleString withImageArray:(NSMutableArray *)imageArrayData
{
    self=[super initWithFrame:frame];
    if (self) {
        self.imageArray=imageArrayData;
        self.titleString=titleString;
        [self getCollectionViewHeight];
        [self createCollectionView];
        [self.collectionView reloadData];
    }
    return self;
}

-(void)createCollectionView{

    [self getCollectionViewHeight];
    UICollectionViewFlowLayout  * layOut =[[UICollectionViewFlowLayout alloc] init];
    layOut.minimumInteritemSpacing =0;
    layOut.minimumLineSpacing = 0;
    layOut.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    _collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth,  _collectionViewHeight) collectionViewLayout:layOut];
    layOut.itemSize=CGSizeMake( (ScreenWidth-30.0)/4.0,  (ScreenWidth-30.0)/4.0);
    _collectionView.collectionViewLayout=layOut;
    _collectionView.backgroundColor=[UIColor whiteColor];
    _collectionView.delegate=self;
    _collectionView.dataSource=self;
    [_collectionView registerClass:[UploadImageCollectionViewCell class] forCellWithReuseIdentifier:[UploadImageCollectionViewCell reuseIdentifier]];
    [_collectionView registerClass:[MM_CollectionHeaderView  class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    [self addSubview:_collectionView];
}
#pragma mark----delegate、dataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imageArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UploadImageCollectionViewCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:kClsID(UploadImageCollectionViewCell) forIndexPath:indexPath];
    
    if (self.imageArray.count-1==indexPath.row) {
       
        NSString  *  imageString=self.imageArray[indexPath.row];
        cell.imageContent.image=[UIImage imageNamed:imageString];
        cell.delectButton.hidden=YES;
    } else {

         cell.delectButton.hidden=NO;
         NSString  *  url=self.imageArray[indexPath.row];
        [cell.imageContent sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",HTTP_URL,url]] placeholderImage:[UIImage imageNamed:@"default_icon"]];
         cell.delectButton.tag = indexPath.row;
        [cell.delectButton addTarget:self action:@selector(deleFraomArray:) forControlEvents:UIControlEventTouchUpInside];
    }
      return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

        MM_CollectionHeaderView * headerV = (MM_CollectionHeaderView *)[collectionView dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"forIndexPath:indexPath];
        headerV.titleNameLb.text=self.titleString;
        if (self.isShowButton==YES) {
            headerV.reUpdateImage.hidden=NO;
        } else {
            headerV.reUpdateImage.hidden=YES;
        }
        BD_WEAKSELF
        [headerV setToupLoadImage:^{
            weakSelf.ToShowAlert();
        }];
        return headerV;
        
    }else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {

        UICollectionReusableView  * footerView=(UICollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"collettionSectionFoot" forIndexPath:indexPath];
        return footerView;
        
    } else {
        
        return nil;
    }
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
    sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat  photoWidth=(ScreenWidth-30.0)/4.0;
    CGSize size=CGSizeMake(photoWidth, photoWidth);
    return size;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(ScreenWidth, 50);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.imageArray.count-1==indexPath.row) {
   
        self.ToShowAlert();
   
      } else {

    }
}
#pragma mark- 照片点击红色按钮删除 、collection高度
-(void)deleFraomArray:(UIButton *)button
{

    self.ToDelectImage(button.tag);
}
-(void)delectImageSure:(NSInteger)tag{
    [self.imageArray removeObjectAtIndex:tag];
    [self getCollectionViewHeight];
    [self.collectionView reloadData];
     self.ToDelectFileUrl(tag);
}
-(void)getCollectionViewHeight{

   CGFloat photoWidth=(ScreenWidth-30.0)/4;
    _collectionViewHeight=0;
    _collectionViewHeight=_collectionViewHeight+50;
    NSInteger   number= self.imageArray.count%4;
    NSInteger   zomeNumber=self.imageArray.count/4;
    if (number== 0){

        number=zomeNumber;

    } else {

        number=zomeNumber+1;
    }
    _collectionViewHeight=_collectionViewHeight+(number*photoWidth);
    _collectionView.height=_collectionViewHeight;
    [_collectionView reloadData];
}
@end
#pragma mark----图片collectionCell;
@implementation  UploadImageCollectionViewCell
-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        [self onInitUI];
    }
    return self;
}
-(void)onInitUI{
    CGFloat  photoWidth=(ScreenWidth-30.0)/4.0;

    self.backgroundColor=[UIColor whiteColor];
    self.imageContent=[[UIImageView   alloc]init];
    self.imageContent.frame=CGRectMake(5, 5, photoWidth-10,  photoWidth-10);
    self.imageContent.contentMode=UIViewContentModeScaleToFill;
    [self addSubview:self.imageContent];
    
    self.delectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.delectButton.frame = CGRectMake(self.imageContent.right-20, self.imageContent.top, 20, 20);
    self.delectButton.hidden = NO;
    self.delectButton.userInteractionEnabled = YES;
    
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    img.image = [UIImage imageNamed:@"delect_icon"];
    [self.delectButton addSubview:img];
    [self addSubview:_delectButton];

}
+ (NSString*)reuseIdentifier{
    
    return @"UploadImageCollectionViewCell";
    
}
@end
#pragma marrk----自定义头部信息
@implementation MM_CollectionHeaderView
-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self){
        
        self.backgroundColor=[UIColor whiteColor];
        self.titleNameLb=[[UILabel alloc] initWithFrame:CGRectMake(15, 8, ScreenWidth-30, 18)];
        self.titleNameLb.textColor=Black_1A1A1A;
        self.titleNameLb.font=[UIFont systemFontOfSize:15.0];
       [self addSubview:self.titleNameLb];
        
        self.reUpdateImage=[UIButton buttonWithType:UIButtonTypeCustom];
        self.reUpdateImage.frame=CGRectMake(ScreenWidth-85, 8, 70, 18);
        [self.reUpdateImage setTitle:@"重新上传" forState:UIControlStateNormal];
         self.reUpdateImage.titleLabel.font=[UIFont systemFontOfSize:12.0];
        [self.reUpdateImage createBordersWithColor:gray808080_color withCornerRadius:9.0 andWidth:0.5];
        [self.reUpdateImage setTitleColor:gray808080_color forState:UIControlStateNormal];
        [self.reUpdateImage  addTarget:self action:@selector(reUploadImageClick) forControlEvents:UIControlEventTouchUpInside];
        self.reUpdateImage.hidden=YES;
        [self addSubview:_reUpdateImage];
        
       self.zhuShiLb=[[UILabel alloc] initWithFrame:CGRectMake(15, self.titleNameLb.bottom+8, ScreenWidth-30, 15)];
       self.zhuShiLb.textColor=gray808080_color;
       self.zhuShiLb.text=@"单张最大不超过512k";
       self.zhuShiLb.font=[UIFont systemFontOfSize:9.0];
       [self addSubview:self.zhuShiLb];
    }
    return self;
}
-(void)reUploadImageClick{
    self.ToupLoadImage();
}
@end
